package com.jacky.ocr_plugin.camera;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager;

import com.jacky.util.OcrLogger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * 来自com.yunmai.cc.idcard.controler.CameraManager
 * 为了处理预览画面问题
 * Created by liaoyacheng on 2021/9/16.
 */
public class OcrCameraManager implements Camera.AutoFocusCallback {

    private final String TAG = "cc_camera";
    private final int mWidth = 1400;
    private int mPreviewWidth = 1280;
    //view的宽或高，用于转为摄像头的宽度
    private int mViewHeightOrWidthForCameraWidth;
    private final Handler mHandler;
    private Camera mCamera;
    private final Context mContext;
    private boolean first = true;
    private int mCameraFace = Camera.CameraInfo.CAMERA_FACING_BACK;
    private final static boolean DEBUG = false;

    private final Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {
        public void onAutoFocus(boolean success, Camera camera) {
            OcrLogger.d(TAG, "---autoFoucs-------------->>" + success);
        }
    };
    private Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
            if (!OcrCameraManager.this.first) {
                Message msg = OcrCameraManager.this.mHandler.obtainMessage();
                msg.what = 200;
                msg.obj = data;
                OcrCameraManager.this.mHandler.sendMessage(msg);
            }
        }
    };

    public void setPreviewCallback(Camera.PreviewCallback previewCallback){
        this.previewCallback = previewCallback;
    }

    public OcrCameraManager(Context context, Handler handler) {
        this.mHandler = handler;
        this.mContext = context;
    }

    public void setViewHeightOrWidthForCameraWidth(int viewHeightOrWidthForCameraWidth) {
        this.mViewHeightOrWidthForCameraWidth = viewHeightOrWidthForCameraWidth;
    }

    @Override
    public void onAutoFocus(boolean success, Camera camera) {
        OcrLogger.d(TAG, "--onAutoFocus------>>>" + success);
    }

    ///获取手机相机方向 https://zhuanlan.zhihu.com/p/110944780
    public int getCameraOrientation(int cameraId){
        Camera.CameraInfo info =new Camera.CameraInfo();
        Camera.getCameraInfo(cameraId,info);
        return info.orientation;
    }

    ///显示方向
    public int getDisplayOrientation(){
        WindowManager windowManager = (WindowManager)this.mContext.getSystemService(Context.WINDOW_SERVICE);
        int or = windowManager.getDefaultDisplay().getRotation();
        switch (or){
            case Surface.ROTATION_90 : return 90;
            case Surface.ROTATION_180 : return 180;
            case Surface.ROTATION_270 : return 270;
            case Surface.ROTATION_0 :
            default:
                return 0;
        }
    }

    ///获取camera画面需要旋转的角度
    ///因为Camera.open()默认后置摄像头
    public int getCameraAngle(){
        //来自 Camera.setDisplayOrientation
        int degrees = getDisplayOrientation();
        int orientation = getCameraOrientation(mCameraFace);
        int result;
        if (mCameraFace == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (orientation + degrees) % 360;
            result = (360 - result) % 360;  // compensate the mirror
        } else {  // back-facing
            result = (orientation - degrees + 360) % 360;
        }
//        OcrLogger.d("tag","flutter to android, getDisplayOrientation " + degrees + " orientation "
//                + orientation+ " result " + result);
        return result;
//        int cameraO = getCameraOrientation(Camera.CameraInfo.CAMERA_FACING_BACK);
//        int displayO = getDisplayOrientation();
//        OcrLogger.d("tag","flutter to android, cameraOrientation " + cameraO + " displayOrientation " + displayO);
//        return (getCameraOrientation(mCameraFace) - getDisplayOrientation() + 360) % 360;
    }

    ///矫正 Camera 的预览角度及图片保存角度
    public void setDisplayOrientation(){
        if(mCamera!= null){
            int angle = getCameraAngle();
            //设置预览角度
            mCamera.setDisplayOrientation(angle);
            //照片保存角度
            Camera.Parameters params = mCamera.getParameters();
            params.setRotation(angle);
            mCamera.setParameters(params);
        }
    }

    public void openCamera(SurfaceHolder holder) throws IOException {
        openCamera(mCameraFace);
//        if (this.mCamera == null) {
//            mCameraFace = Camera.CameraInfo.CAMERA_FACING_BACK;
//            this.mCamera = Camera.open();
//        }
        this.mCamera.setPreviewDisplay(holder);
        this.setWhiteBalanceAuto();
    }

    public boolean cameraOpened() {
        return this.mCamera != null;
    }

    public void openCamera() {
        //if (this.mCamera == null) { }
        openCamera(mCameraFace);
//            mCameraFace = Camera.CameraInfo.CAMERA_FACING_BACK;
//            this.mCamera = Camera.open();
        try {
            this.mCamera.setOneShotPreviewCallback(this.previewCallback);
            initDisplay();
        } catch (Exception var2) {
            var2.printStackTrace();
        }
    }

    public void setPreviewDisplay(SurfaceHolder holder) {
        if (this.mCamera != null) {
            try {
                this.mCamera.setPreviewDisplay(holder);
            } catch (IOException var3) {
                var3.printStackTrace();
            }
        }

    }

    public boolean initDisplay() {
        if (this.mCamera != null) {
            try {
                this.mCamera.startPreview();
                return true;
            } catch (Exception var2) {
                var2.printStackTrace();
            }
        }
        return false;
    }

    public void pauseDisplay() {
        if (this.mCamera != null) {
            this.mCamera.stopPreview();
        }

    }

    public boolean isCameraFacingBack(){
        return mCameraFace == Camera.CameraInfo.CAMERA_FACING_BACK;
    }

    public void takePicture(Camera.PictureCallback callback){
        if(mCamera != null){
            mCamera.takePicture(null, null, callback);
        }
    }
    ///根据前后摄像头，打开对应摄像头，如果没找到，默认打开后置摄像头
    private void openCamera(int cameraId){
        this.closeCamera();
        try {
            int numberOfCameras = Camera.getNumberOfCameras();
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            for (int i = 0; i < numberOfCameras; i++) {
                Camera.getCameraInfo(i, cameraInfo);
                if (cameraInfo.facing == cameraId) {
                    mCameraFace = cameraId;
                    mCamera = Camera.open(i);
                    break;
                }
            }
            if(mCamera == null){
                mCameraFace = Camera.CameraInfo.CAMERA_FACING_BACK;
                this.mCamera = Camera.open();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void switchCamara(SurfaceHolder holder){
        if(mCamera == null) return;
        int cameraFace = mCameraFace == Camera.CameraInfo.CAMERA_FACING_BACK
                ? Camera.CameraInfo.CAMERA_FACING_FRONT
                : Camera.CameraInfo.CAMERA_FACING_BACK;
        openCamera(cameraFace);
        try {
            if(mCameraFace == Camera.CameraInfo.CAMERA_FACING_BACK){
                setCameraFlashModel(Camera.Parameters.FLASH_MODE_OFF);
            }
            setPreviewSize();
            setDisplayOrientation();
            this.mCamera.setOneShotPreviewCallback(this.previewCallback);
            this.mCamera.setPreviewDisplay(holder);
            initDisplay();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void closeCamera() {
        if (this.mCamera != null) {
            this.mCamera.stopPreview();
            this.mCamera.release();
            this.mCamera = null;
        }

    }

    public void setCameraFlashModel(String model) {
        if (this.mCamera != null) {
            Camera.Parameters parameters = this.mCamera.getParameters();
            parameters.setFlashMode(model);

            try {
                this.mCamera.setParameters(parameters);
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

    }

    public boolean isSupportAutoFocus() {
        List<String> list = this.getSupportedFocusModes();
        if (list == null) {
            return false;
        } else {
            Iterator var3 = list.iterator();

            while(var3.hasNext()) {
                String string = (String)var3.next();
                if (Camera.Parameters.FOCUS_MODE_AUTO.equals(string)) {
                    this.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                    return true;
                }
            }

            return false;
        }
    }

    public void requestFoucs() {
        if (this.mCamera != null) {
            this.mCamera.autoFocus(this);
        }

    }

    public void setFocusMode(String mode) {
        if (this.mCamera != null) {
            try {
                Camera.Parameters parameters = this.mCamera.getParameters();
                parameters.setFocusMode(mode);
                this.mCamera.setParameters(parameters);
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

    }

    public void setPreviewSize(int width, int height) {
        try {
            Camera.Parameters parameters = this.mCamera.getParameters();
            parameters.setPreviewSize(width, height);
            this.mCamera.setParameters(parameters);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

    }

    public void setPicSize(Camera.Size optimalSize) {
        if (this.mCamera != null) {
            Camera.Parameters parameters = this.mCamera.getParameters();
            parameters.setPictureFormat(256);//ImageFormat.JPEG
            List<Camera.Size> pictureSize = parameters.getSupportedPictureSizes();
            if (optimalSize != null) {
                parameters.setPictureSize(optimalSize.width, optimalSize.height);
                parameters.setJpegQuality(100);
                this.mCamera.setParameters(parameters);
                OcrLogger.d(TAG, optimalSize.width + "<--w-optimalPicSize---h---->" + optimalSize.height);
                return;
            }

            int x = 0;
            int tempSize = 3000;

            for(int i = 0; i < pictureSize.size(); ++i) {
                if (((Camera.Size)pictureSize.get(i)).width >= this.mWidth && ((Camera.Size)pictureSize.get(i)).width <= tempSize) {
                    tempSize = ((Camera.Size)pictureSize.get(i)).width;
                    x = i;
                }
            }

            parameters.setPictureSize(((Camera.Size)pictureSize.get(x)).width, ((Camera.Size)pictureSize.get(x)).height);
            parameters.setJpegQuality(100);
            this.mCamera.setParameters(parameters);
            OcrLogger.d(TAG, ((Camera.Size)pictureSize.get(x)).width + "<--w-pictureSize---h---->" + ((Camera.Size)pictureSize.get(x)).height);
        }

    }

    private Camera.Size getPreviewSizes() {
        Camera.Size pSize = this.getPictureSizeForPreviewSize();
        if (pSize == null) {
            this.setPicSize((Camera.Size)null);
            Camera.Parameters var2 = this.mCamera.getParameters();
            List prviewSizes = var2.getSupportedPreviewSizes();
            if (prviewSizes != null) {
                WindowManager windowManager = (WindowManager)this.mContext.getSystemService(Context.WINDOW_SERVICE);
                Display display = windowManager.getDefaultDisplay();
                int w = display.getWidth();
                if (w > this.mPreviewWidth) {
                    this.mPreviewWidth = w;
                    return null;
                } else {
                    int x = 0;
                    int tempSize = 0;

                    for(int i = 0; i < prviewSizes.size(); ++i) {
                        if (((Camera.Size)prviewSizes.get(i)).width <= this.mPreviewWidth && ((Camera.Size)prviewSizes.get(i)).width >= tempSize) {
                            tempSize = ((Camera.Size)prviewSizes.get(i)).width;
                            x = i;
                        }
                    }

                    return (Camera.Size)prviewSizes.get(x);
                }
            } else {
                return null;
            }
        } else {
            return pSize;
        }
    }

    private Camera.Size getPictureSizeForPreviewSize() {
        Camera.Parameters parameters = this.mCamera.getParameters();
        List<Camera.Size> prviewSizes = parameters.getSupportedPreviewSizes();
        List<Camera.Size> pictureSize = parameters.getSupportedPictureSizes();
        List<Camera.Size> tempMaxSizes = new ArrayList();
        List<Camera.Size> tempMinSizes = new ArrayList();

        Camera.Size tempSize;
        int i;
        int j;
        for(i = pictureSize.size(); i >= 0; --i) {
            for(j = 0; j < i - 1; ++j) {
                if (((Camera.Size)pictureSize.get(j)).width > ((Camera.Size)pictureSize.get(j + 1)).width) {
                    tempSize = (Camera.Size)pictureSize.get(j + 1);
                    pictureSize.set(j + 1, (Camera.Size)pictureSize.get(j));
                    pictureSize.set(j, tempSize);
                }
            }
        }

        for(i = 0; i < pictureSize.size(); ++i) {
            if (((Camera.Size)pictureSize.get(i)).width >= this.mWidth) {
                tempMaxSizes.add((Camera.Size)pictureSize.get(i));
            } else {
                tempMinSizes.add((Camera.Size)pictureSize.get(i));
            }
        }

        for(i = prviewSizes.size(); i >= 0; --i) {
            for(j = 0; j < i - 1; ++j) {
                if (((Camera.Size)prviewSizes.get(j)).width > ((Camera.Size)prviewSizes.get(j + 1)).width) {
                    tempSize = (Camera.Size)prviewSizes.get(j + 1);
                    prviewSizes.set(j + 1, (Camera.Size)prviewSizes.get(j));
                    prviewSizes.set(j, tempSize);
                }
            }
        }

        for(i = prviewSizes.size() - 1; i >= 0; --i) {
            if (((Camera.Size)prviewSizes.get(i)).width <= this.mPreviewWidth) {
                for(j = 0; j < tempMaxSizes.size(); ++j) {
                    if ((double)((Camera.Size)prviewSizes.get(i)).width / (double)((Camera.Size)prviewSizes.get(i)).height == (double)((Camera.Size)tempMaxSizes.get(j)).width / (double)((Camera.Size)tempMaxSizes.get(j)).height) {
                        this.setPicSize((Camera.Size)tempMaxSizes.get(j));
                        return (Camera.Size)prviewSizes.get(i);
                    }
                }
            }
        }

        for(i = prviewSizes.size() - 1; i >= 0; --i) {
            if (((Camera.Size)prviewSizes.get(i)).width <= this.mPreviewWidth) {
                for(j = tempMinSizes.size() - 1; j >= 0; --j) {
                    if (((Camera.Size)prviewSizes.get(i)).width / ((Camera.Size)prviewSizes.get(i)).height == ((Camera.Size)tempMinSizes.get(j)).width / ((Camera.Size)tempMinSizes.get(j)).height) {
                        this.setPicSize((Camera.Size)tempMinSizes.get(j));
                        return (Camera.Size)prviewSizes.get(i);
                    }
                }
            }
        }

        return null;
    }

    public void setPreviewSizeOld() {
        if (this.mCamera != null) {
            Camera.Size size = this.getPreviewSizes();
            Camera.Parameters parameters = this.mCamera.getParameters();
            if (size != null) {
                OcrLogger.d(TAG, size.width + "<-------setPreviewSize--------->" + size.height);
                parameters.setPreviewSize(size.width, size.height);
            } else {
                WindowManager windowManager = (WindowManager)this.mContext.getSystemService(Context.WINDOW_SERVICE);
                Display display = windowManager.getDefaultDisplay();
                int w = display.getWidth();
                int h = display.getHeight();
                parameters.setPreviewSize(w, h);
            }

            try {
                this.mCamera.setParameters(parameters);
            } catch (Exception var7) {
                var7.printStackTrace();
            }

        }
    }

    public void setPreviewSize(){
        if(mCamera == null)return;
        Camera.Parameters param = this.mCamera.getParameters();
        float rate;
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        int height = mViewHeightOrWidthForCameraWidth;
        if(height == 0) height = mWidth;
        if(mContext.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
            rate = 1.0f * dm.heightPixels / dm.widthPixels; //屏幕的宽高比
        }else {
            rate = 1.0f * dm.widthPixels / dm.heightPixels; //屏幕的宽高比
        }
        Camera.Size p = getFitSize(param.getSupportedPictureSizes(), height, rate);
        if(p != null)  param.setPictureSize(p.width, p.height);

//        StringBuilder sb = new StringBuilder();
//        for(Camera.Size one : param.getSupportedPreviewSizes()){
//            sb.append("width ").append(one.width).append(" height ").append(one.height).append("\n");
//        }
//        OcrLogger.d("tag","height "+height+" rate "+rate+" preview "+sb.toString());
        //屏幕预览只能选择跟手机屏幕接近的大小，才不会变形
        Camera.Size p2 = getFitSize(param.getSupportedPreviewSizes(), height, rate);
        if(p2 != null)  param.setPreviewSize(p2.width, p2.height);
        if(p == null || p2 == null){
            OcrLogger.d("tag","------pictureSize width "+(p != null? p.width:"")
                    +" height "+ (p != null? p.height:"")
                    + " previewSize width " + (p2 != null ? p2.width : "")+" height " + (p2 != null ? p2.height : ""));
            //出现获取Camera.Size问题，使用旧方式
            setPreviewSizeOld();
            return;
        }
        OcrLogger.d("tag","------pictureSize width "+p.width
                +" height "+ p.height
                + " previewSize width " + p2.width+" height " + p2.height);
        try {
            this.mCamera.setParameters(param);
        } catch (Exception var7) {
            var7.printStackTrace();
        }
    }

    /**
     *  获取相机最适合使用的分辨率
     */
    private Camera.Size getFitSize(List<Camera.Size> sizeList, int h, float rate) {
        //size的width和height是横屏状态的高宽，竖屏下需要对换下
        int width = h;

        int offset = Integer.MAX_VALUE;
        Camera.Size ss = null;
        for(Camera.Size size : sizeList) {
            if(size.width >= width && equalRate(size, rate)) {
                if((size.width - width) < offset) {
                    offset = size.width - width;
                    ss = size;
                }
            }
        }
        return ss;
    }

    private boolean equalRate(Camera.Size s, float rate) {
        float r = (float) (s.width) / (float) (s.height);
        if (Math.abs(r - rate) <= 0.2) {
            return true;
        } else {
            return false;
        }
    }
    public void autoFocusAndPreviewCallback() {
        if (this.mCamera != null) {
            try {
                this.first = false;
                this.mCamera.setOneShotPreviewCallback(this.previewCallback);
            } catch (Exception var2) {
                var2.printStackTrace();
                this.mHandler.sendEmptyMessage(206);
            }
        }

    }

    public void autoFoucs() {
        try {
            this.mCamera.autoFocus(this.autoFocusCallback);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    private List<String> getSupportedFocusModes() {
        List<String> list = null;
        if (this.mCamera != null) {
            Camera.Parameters parameters = this.mCamera.getParameters();
            list = parameters.getSupportedFocusModes();
            Iterator var4 = list.iterator();

            while(var4.hasNext()) {
                String string = (String)var4.next();
                OcrLogger.d("path", "------SupportedFocusModes----------->>" + string);
            }
        }

        return list;
    }

    private void setWhiteBalanceAuto() {
        if (this.mCamera != null) {
            Camera.Parameters parameters = this.mCamera.getParameters();
            parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);

            try {
                this.mCamera.setParameters(parameters);
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

    }

    public Rect getViewfinder(Rect finderRect) {
        WindowManager windowManager = (WindowManager)this.mContext.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        float w = (float)display.getWidth();
        float h = (float)display.getHeight();
        int width = this.mCamera.getParameters().getPreviewSize().width;
        int height = this.mCamera.getParameters().getPreviewSize().height;
        float xs = (float)width / w;
        float ys = (float)height / h;
        Rect rect = new Rect(finderRect);
        rect.left = (int)((float)finderRect.left * xs);
        rect.right = (int)((float)finderRect.right * xs);
        rect.top = (int)((float)finderRect.top * ys);
        rect.bottom = (int)((float)finderRect.bottom * ys);
        return rect;
    }

    public Rect getRectByPhoto(Rect finderRect) {
        Rect previewRect = this.getViewfinder(finderRect);
        float w = (float)this.mCamera.getParameters().getPreviewSize().width;
        float h = (float)this.mCamera.getParameters().getPreviewSize().height;
        float width = (float)this.mCamera.getParameters().getPictureSize().width;
        float height = (float)this.mCamera.getParameters().getPictureSize().height;
        float xs = width / w;
        float ys = height / h;
        Rect rect = new Rect(previewRect);
        OcrLogger.d("ocr", rect.left + "<-----1--->" + rect.right + "<----->" + rect.top + "<------------>" + rect.bottom);
        rect.left = (int)((float)previewRect.left * xs);
        rect.right = (int)((float)previewRect.right * xs);
        rect.top = (int)((float)previewRect.top * ys);
        rect.bottom = (int)((float)previewRect.bottom * ys);
        OcrLogger.d("ocr", rect.left + "<-----2--->" + rect.right + "<----->" + rect.top + "<------------>" + rect.bottom);
        return rect;
    }

    public List<Camera.Size> getSupportedPreviewSizes() {
        return this.mCamera != null ? this.mCamera.getParameters().getSupportedPreviewSizes() : null;
    }

    public Camera.Size getPicSize() {
        return this.mCamera != null ? this.mCamera.getParameters().getPictureSize() : null;
    }

    public int getPreviewWidth() {
        return this.mCamera != null ? this.mCamera.getParameters().getPreviewSize().width : 0;
    }

    public int getPreviewHeight() {
        return this.mCamera != null ? this.mCamera.getParameters().getPreviewSize().height : 0;
    }

    public Camera.Size getOptimalPreviewSize(Activity currentActivity, List<Camera.Size> sizes, double targetRatio) {
        double ASPECT_TOLERANCE = 0.001D;
        if (sizes == null) {
            return null;
        } else {
            Camera.Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;//1.7976931348623157E308D;
            Display display = currentActivity.getWindowManager().getDefaultDisplay();
            int targetHeight = Math.min(display.getHeight(), display.getWidth());
            if (targetHeight <= 0) {
                targetHeight = display.getHeight();
            }

            Iterator var13 = sizes.iterator();

            Camera.Size size;
            while(var13.hasNext()) {
                size = (Camera.Size)var13.next();
                double ratio = (double)size.width / (double)size.height;
                if (Math.abs(ratio - targetRatio) <= 0.001D && (double)Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = (double)Math.abs(size.height - targetHeight);
                }
            }

            if (optimalSize == null) {
                OcrLogger.w(TAG, "No preview size match the aspect ratio");
                minDiff = Double.MAX_VALUE;//1.7976931348623157E308D;
                var13 = sizes.iterator();

                while(var13.hasNext()) {
                    size = (Camera.Size)var13.next();
                    if ((double)Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = (double)Math.abs(size.height - targetHeight);
                    }
                }
            }

            return optimalSize;
        }
    }

    public Camera.Size getOptimalPictureSize(List<Camera.Size> sizes, double targetRatio, boolean isMax) {
        int tempMax = 100000000;
        int tempMin = 0;
        Camera.Size s = null;
        double ASPECT_TOLERANCE = 0.001D;
        if (sizes == null) {
            return null;
        } else {
            Iterator var11 = sizes.iterator();

            while(var11.hasNext()) {
                Camera.Size size = (Camera.Size)var11.next();
                OcrLogger.d(TAG, size.width + "<-------getOptimalPictureSize---------->" + isMax);
                double ratio = (double)size.width / (double)size.height;
                if (ratio == targetRatio) {
                    if (isMax) {
                        if (size.width * size.height < tempMax) {
                            tempMax = size.width * size.height;
                            s = size;
                        }
                    } else if (size.width * size.height > tempMin) {
                        tempMin = size.width * size.height;
                        s = size;
                    }
                }
            }

            return s;
        }
    }

    public boolean openFlashlight() {
        if (this.mCamera == null) {
            mCameraFace = Camera.CameraInfo.CAMERA_FACING_BACK;
            this.mCamera = Camera.open();
        }

        Camera.Parameters parameters = this.mCamera.getParameters();
        if (parameters.getSupportedFlashModes() == null) {
            return false;
        } else if (parameters.getFlashMode().equalsIgnoreCase( Camera.Parameters.FLASH_MODE_TORCH)) {
            return false;
        } else {
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

            try {
                this.mCamera.setParameters(parameters);
            } catch (Exception var3) {
                var3.printStackTrace();
            }

            return this.mCamera.getParameters().getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH);
        }
    }

    public boolean closeFlashlight() {
        if (this.mCamera == null) {
            mCameraFace = Camera.CameraInfo.CAMERA_FACING_BACK;
            this.mCamera = Camera.open();
        }

        if (this.mCamera.getParameters().getFlashMode().equalsIgnoreCase(Camera.Parameters.FLASH_MODE_OFF)) {
            return false;
        } else {
            Camera.Parameters parameters = this.mCamera.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

            try {
                this.mCamera.setParameters(parameters);
            } catch (Exception var3) {
                var3.printStackTrace();
            }

            return this.mCamera.getParameters().getFlashMode().equalsIgnoreCase(Camera.Parameters.FLASH_MODE_OFF);
        }
    }


    ///修改previewSize

}
